package com.octoplay.player;

import android.app.Activity;
import android.net.Uri;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.webkit.ConsoleMessage;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class FlashMediaPlayer extends BaseMediaPlayer {

	public static final String LOGTAG = "FlashMediaPlayer";

	private int mediaPosition = 0;
	private int mediaDuration = 0;
	private WebView mWebView;

	private PlayerListener listener;
	private int port;

	public FlashMediaPlayer(Activity mActivity,
			final PlayerListener listener, int port) {

		this.mActivity = mActivity;
		this.listener = listener;
		this.port = port;

		mWebView = new WebView(mActivity);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.getSettings().setPluginsEnabled(true);
		mWebView.getSettings().setUseWideViewPort(true);
		mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
		mWebView.addJavascriptInterface(new JavaScriptInterface(), "ANDROID");
		mWebView.setWebChromeClient(new WebChromeClient() {
			public boolean onConsoleMessage(ConsoleMessage cm) {
				Log.d(LOGTAG, cm.message() + " -- From line " + cm.lineNumber()
						+ " of " + cm.sourceId());
				return true;
			}
		});
		mWebView.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				view.loadUrl(url);
				return true;
			}
		});
		mWebView.setOnTouchListener(new OnTouchListener() {
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN)
					listener.onPlayerClicked(FlashMediaPlayer.this);
				return false;
			}
		});

	}

	@Override
	public void prepare() {
		mWebView.loadUrl("http://localhost:" + port + "/player.html");
	}

	@Override
	public void requestNativeSeek(int seekTo) {
		// TODO RTMP SEEK
	}

	@Override
	public void pause() {
		mWebView.loadUrl("javascript:pause()");
		STATUS = STATUS_PAUSED;
	}

	@Override
	public void start() {
		mWebView.loadUrl("javascript:start('" + playUrl + "')");
	}

	@Override
	public void play() {
		mWebView.loadUrl("javascript:play()");
	}

	@Override
	public void stop() {
		mWebView.loadUrl("javascript:stop()");
	}

	@Override
	public int getCurrentPosition() {
		return mediaPosition;
	}

	@Override
	public int getDuration() {
		return mediaDuration;
	}

	@Override
	public void release() {
		reset();
		mWebView.destroy();
	}

	@Override
	public void reset() {

		this.mediaPosition = 0;
		this.mediaDuration = 0;

		STATUS = STATUS_IDLE;
		playUrl = null;
		this.stop();
	}

	// JAVA SCRIPT CALL BACK INTERFACE
	class JavaScriptInterface {

		// CALLBACKS FROM JAVA-SCRIPT
		public void showToast(String toast) {
			Toast.makeText(mActivity, toast, Toast.LENGTH_SHORT).show();
		}

		public void onSeekComplete() {
			// listener.onNativeSeekComplete(FlashMediaPlayer.this);
		}

		public void onStop() {
			STATUS = STATUS_STOPPED;
		}

		public void onCompletion() {
			STATUS = STATUS_PLAYBACKCOMPLETED;
			listener.onPlaybackComplete(FlashMediaPlayer.this);
		}

		public void onDuration(int duration) {
			mediaDuration = duration;
		}

		public void onCurrent(int current) {
			mediaPosition = current;
		}

		public void onDebug(String dbg) {
			Log.d("ACTIONSCRIPT", dbg);
		}

		public void onPlay() {
			STATUS = STATUS_STARTED;
		}

		public void onPause() {
			STATUS = STATUS_PAUSED;
		}

		public void onPlayerReady() {
			mActivity.runOnUiThread(new Runnable() {
				public void run() {
					// mWebView.loadUrl("javascript:enableDebug('true')");
					FlashMediaPlayer.this.listener
							.onPrepared(FlashMediaPlayer.this);
					STATUS = STATUS_IDLE;
				}
			});
		}
	}

	@Override
	public boolean isPrepared() {
		return STATUS >= STATUS_PREPARED ? true : false;
	}

	@Override
	public boolean isPlaying() {
		return STATUS == STATUS_STARTED ? true : false;
	}

	@Override
	public boolean isPaused() {
		return STATUS == STATUS_PAUSED ? true : false;
	}

	@Override
	public View getView() {
		return mWebView;
	}

	@Override
	public void setDataSource(Uri url) {
		playUrl = url;
		STATUS = STATUS_INITIALIZED;
	}
}